package cn.gz.demo.ui;


import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.ArrayList;

import cn.gz.demo.R;
import cn.gz.demo.adapter.ListViewAdapter;
import cn.gz.demo.databinding.SwiperefreshlayoutBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2016/12/6.
 */
public class SwipeRefreshLayoutActivity extends BaseActivity {
    private SwiperefreshlayoutBinding binding;
    private ArrayList<String> list = new ArrayList<String>();
    private ListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_swiperefreshlayout);
        initView();
        initData();
    }

    private void initView() {

        binding.titleLayout.titleName.setText("SwipeRefreshLayout");
        binding.titleLayout.titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
//                overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
            }
        });
        // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
        binding.swipeContainer.setColorSchemeResources(R.color.blue, R.color.green,
                R.color.orange, R.color.yellow);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        binding.swipeContainer.setDistanceToTriggerSync(200);
        // 设定下拉圆圈的背景
        binding.swipeContainer.setProgressBackgroundColor(R.color.white);
        // 设置圆圈的大小
        binding.swipeContainer.setSize(SwipeRefreshLayout.DEFAULT);

        adapter = new ListViewAdapter(this, list);
        binding.listview.setAdapter(adapter);

        binding.swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        binding.swipeContainer.setRefreshing(false);
                        initData();
                    }
                }, 5000);
            }
        });
    }

    private void initData() {
        list.add("Hello");
        list.add("This is stormzhang");
        list.add("An Android Developer");
        list.add("Love Open Source");
        list.add("My GitHub: stormzhang");
        list.add("weibo: googdev");
        adapter.notifyDataSetChanged();
    }

}
